package code.codeworld.common.kits;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.io.Serial;

/**
 * @Description: 自定义异常
 * @Author: codeworld
 * @Date: 2023/10/11 14:20
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class Exceptions extends RuntimeException {
    @Serial
    private static final long serialVersionUID = 1L;

    private int code;
    private String msg;

    public Exceptions(Exception e) {
        super(e);
        this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode();
        this.msg = e.getMessage();
    }

    public Exceptions(String msg) {
        super(msg);
        this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode();
        this.msg = msg;
    }

    public Exceptions(ErrorCode errorCode) {
        super(errorCode.getMsg());
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }


    /**
     * 错误编码
     */
    @Getter
    @AllArgsConstructor
    public static enum ErrorCode {
        BAD_REQUEST(400, "请求参数错误"),
        REFRESH_TOKEN_INVALID(400, "refresh_token 已失效"),
        UNAUTHORIZED(401, "还未授权，不能访问"),
        FORBIDDEN(403, "没有权限，禁止访问"),
        RUN_ERROR(500, "执行异常，请稍后再试"),
        INTERNAL_SERVER_ERROR(500, "服务器异常，请稍后再试");

        private final int code;
        private final String msg;
    }


}
